package com.steven.sync.mianshi;


import java.util.LinkedList;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 固定同步容器，put get ，getcount方法，支持2个生产者，10个消费者调用 阻塞调用
 * Condition :有多个等待队列
 */
public class Test2_p_c_Blocking {

    static LinkedList list = new LinkedList<>();

    static final private Integer MAX =10;


    private final BlockingDeque blockingDeque = new LinkedBlockingDeque(10);


    public void  put (Object o){
        blockingDeque.add(o);
        System.out.println(Thread.currentThread().getName()+"生产");
    }


    public void  get (){
        try {
            blockingDeque.take();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"消费");
    }
    public Integer  getCount(){
       return list.size();
    }

    public static void main(String[] args) {


        Test2_p_c_Blocking test2Pcnotify = new Test2_p_c_Blocking();

        //生产者
        for (int i = 0; i < 2; i++) {
            new Thread(() -> {
                for (int j = 0; j < 25; j++) {
                    test2Pcnotify.put(new Object());
                }
            }, "P" + i).start();
        }

        //消费者
        for (int i = 0; i < 9; i++) {
            new Thread(() -> {
                for (int j = 0; j < 25; j++) {
                    test2Pcnotify.get();
                }
            }, "C" + i).start();
        }



    }




}
